package com.rover.controller.system;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.grid.Page;
import com.rover.entity.system.Role;
import com.rover.service.system.RoleService;
import com.utils.AppUtil;

/**
 * 角色
 */
@Controller
@RequestMapping("/roleController")
public class RoleController {

	@Autowired
	private RoleService roleService;

	@RequestMapping(value = "/query")
	public @ResponseBody
	Map<String, Object> query(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> params = new HashMap<String, Object>();
		String hql = addWhere(request, params);
		Page<Role> page = new Page<Role>(request);
		page = roleService.query(page, hql, params);
		map = AppUtil.buildJSONDataResponse(page.getResult(),
				page.getTotalCount());
		return map;
	}
	
	@RequestMapping(value = "/getList")
	public @ResponseBody
	Map<String, Object> getList(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		map = AppUtil.buildJSONDataResponse(roleService.getList(),
				0l);
		return map;
	}

	private String addWhere(HttpServletRequest request,
			Map<String, Object> params) {
		String hql = " where 1=1 ";
		if (request.getParameter("name") != null) {
			hql += " and name like :name";
			params.put("name", "%%" + request.getParameter("name") + "%%");
		}
		// if (user.getCreatedatetimeEnd() != null) {
		// hql += " and t.createdatetime <= :getCreatedatetimeEnd";
		// params.put("getCreatedatetimeEnd", user.getCreatedatetimeEnd());
		// }
		// if (user.getModifydatetimeStart() != null) {
		// hql += " and t.modifydatetime >= :getModifydatetimeStart";
		// params.put("getModifydatetimeStart", user.getModifydatetimeStart());
		// }
		// if (user.getModifydatetimeEnd() != null) {
		// hql += " and t.modifydatetime <= :getModifydatetimeEnd";
		// params.put("getModifydatetimeEnd", user.getModifydatetimeEnd());
		// }
		// if (user.getQ() != null && !user.getQ().trim().equals("")) {
		// hql += " and t.name like :name ";
		// params.put("name", "%%" + user.getQ().trim() + "%%");
		// }
		// if (user.getName() != null && !user.getName().trim().equals("")) {
		// hql += " and t.name like :name";
		// params.put("name", "%%" + user.getName().trim() + "%%");
		// }
		return hql;
	}

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> save(Role role, HttpServletRequest request)
			throws Exception {
		// UserSession u = (UserSession)
		// request.getSession().getAttribute("userSession");
		Map<String, Object> map = new HashMap<String, Object>();
		Boolean success = true;
		String message = "";
		try {
			if (role.getId() == null) {
				if (roleService.isAddNameRepeat(role.getName())) {
					success = false;
					message = "此角色名称已存在，请重新输入！";
				}
			} else if (roleService.isEditNameRepeat(role.getName(),
					role.getId())) {
				success = false;
				message = "此角色名称已存在，请重新输入！";
			}
			// 判断是否验证成功
			if (success) {
				this.roleService.save(role);
              
			}
			map.put("success", success);
			map.put("message", message);
		} catch (Exception e) {
			// logger.error(e.getMessage(), e);
			map.put("success", false);
			map.put("message", e.getMessage());
		}
		return map;
	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> delete(@RequestParam("id") String id) {
		Map<String, Object> map = new HashMap<String, Object>();
		// String id = request.getParameter("id");
		try {
			roleService.delete(Long.valueOf(id));
			map.put("success", true);
			map.put("message", "");

		} catch (Exception e) {
			map.put("success", false);
			map.put("message", e.getMessage());
		}
		return map;
	}
	@RequestMapping(value = "/getRoleResource", method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> getRoleResource(@RequestParam("id") String id) {
		Map<String, Object> map = new HashMap<String, Object>();
		
		try {
			map.put("success", true);
			map.put("data", roleService.getRoleResourceName(Long.valueOf(id)));

		} catch (Exception e) {
			map.put("success", false);
			map.put("message", e.getMessage());
		}
		return map;
	}
}
